/**
*      author: Y_Yao
*      created: 2025-03-26 20:53:37
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
string S,T;
int n;

void solve()
{
    cin >> n;
    cin >> S >> T;
    S = " " + S;
    T = " " + T;
    for(int i = 1;i <= n;i++)
    {
        if(T[i] == 'A') break;
        if(S[i] == 'A')
        {
            cout << -1 << endl;
            return;
        }
    }

    for(int i = n;i >= 1;i--)
    {
        if(T[i] == 'B') break;
        if(S[i] == 'B')
        {
            cout << -1 << endl;
            return;
        }
    }

    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        if(S[i] != T[i]) ans++;
    }

    int cnt = 0;
    for(int i = 1;i <= n;i++)
    {
        if(S[i] == T[i]) continue;
        if(T[i] == 'A') cnt++;
        if(T[i] == 'B' && cnt)
        {
            cnt--;
            ans--;
        }
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}